/*
 * test_casinfo.cpp
 *  CascadeInfo二进制格式分类器数据文件转json格式的工具
 *  Created on: 2016年3月14日
 *      Author: guyadong
 */
#include <iostream>
#include <sstream>
//#include <regex>
#include <unordered_map>
#include <algorithm> 
#include "cmdline.h"
#include "file_utilits.h"
#include "data2source.h"
using namespace gdface;
using namespace std;
#define VAR_PREFIX "filenames_width_suffix_"
/* 替换字符中的.为_
 * */
//inline std::string replase_dot(std::string src) {
//	return std::regex_replace(src, std::regex("(\\.)"), "_");
//}
void make_var_array(std::ostream &os, std::string src_file) {	
	std::string var_name = replase_dot(tolower(get_file_name(src_file)));
	auto str = make_var_array(var_name, src_file);
	os.write(str.data(), str.size());
}
using suffix_map_type = std::unordered_map<std::string, std::vector<std::string>>;
using key_set_type = std::unordered_set<std::string>;
using file_set_type = std::unordered_set<std::string>;

void make_filename_array_vars_by_suffix(std::ostream &os, const key_set_type&keys, const suffix_map_type &suffix_map) {
	std::for_each(keys.begin(), keys.end(), [&](const key_set_type::key_type &suffix) {
		os << make_string_array_var(VAR_PREFIX+suffix, suffix_map.find(suffix)->second) << std::endl;
	});
}
int main(int argc, char * argv[]){
	try{
		cmdline::parser parser;
		parser.add<std::string>("input", 'i', "src file/folder", true); // 源文件/路径 
		parser.add<std::string>("output", 'o', "output folder", false,"."); // 输出文件路径，默认当前目录 
		parser.add<std::string>("suffix", '\0', "suffix of output file", false,".h"); // 输出文件后缀，默认.h 
		parser.parse_check(argc, argv);
		auto src_file=parser.get<string>("input");
		auto src_is_folder = gdface::is_folder(src_file);
		auto output_folder = parser.get<string>("output");
		if (output_folder==".")output_folder = getcwd();
		std::string output_file_name = replase_dot(tolower(get_file_name(src_file))).append(parser.get<string>("suffix"));
		
		string output_file=path_concate(output_folder, output_file_name);
		gdface::raii_var<std::ofstream> raii_ofs(
				[&](){return std::ofstream(output_file, std::ofstream::binary);},
				[](std::ofstream&ofs){ofs.close();});
		if(src_is_folder){
			suffix_map_type suffix_map;
			file_set_type fileset;
			key_set_type suffixs;
			*raii_ofs<<make_head();
			for_each_file(src_file,
				[&](const char*path,const char* name){
					auto full_path=string(path).append({file_sepator()}).append(name);
					make_var_array(*raii_ofs, full_path);
					fileset.insert(name);
					auto suffix=get_file_suffix(name);
					if(!suffix.empty()){
						auto f=suffix_map.find(suffix);
						if(f==suffix_map.end()){
							suffix_map.emplace(suffix,std::vector<std::string>{name});
							suffixs.insert(suffix);
						}else{
							f->second.emplace_back(name);
						}
					}
				return false;
			},true);
			make_filename_array_vars_by_suffix(*raii_ofs, suffixs, suffix_map);
			*raii_ofs<<make_data_info_map_var("data_info_map",fileset)<<std::endl;
		}
		else {
			make_var_array(*raii_ofs, src_file);
		}
		std::cout << output_file << " saved,size=" << (*raii_ofs).tellp() <<" bytes"<< std::endl;
	}
	catch (std::exception &e) {
		std::cout << e.what() << std::endl;
	}
}


